/*
 * @(#)InitUserView.java		       Project:SpikeSC
 * Date:2012-7-24
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.spikesc.ui;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.log4j.PropertyConfigurator;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import com.sinaapp.msdxblog.spikesc.logic.UserLogic;

//VS4E -- DO NOT REMOVE THIS LINE!
/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class InitUserView extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel jLPasswordConfirm;
	private JLabel jLUsername;
	private JLabel jLPassword;
	private JTextField jTFUsername;
	private JPasswordField jPasswordField;
	private JPasswordField jPasswordFieldConfirm;
	private JButton jBtConfirm;
	private JLabel jLPasswordConfirmTip;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
	public InitUserView() {
		initComponents();
	}

	private void initComponents() {
		setTitle("初始化管理员");
		setLayout(new GroupLayout());
		add(getJLPasswordConfirm(), new Constraints(new Leading(15, 10, 10),
				new Leading(112, 14, 10, 10)));
		add(getJLUsername(), new Constraints(new Leading(27, 10, 10),
				new Leading(35, 10, 10)));
		add(getJLPassword(), new Constraints(new Leading(27, 10, 10),
				new Leading(72, 10, 10)));
		add(getJTFUsername(), new Constraints(new Leading(84, 175, 10, 10),
				new Leading(32, 10, 10)));
		add(getJPasswordField(), new Constraints(new Leading(84, 174, 10, 10),
				new Leading(69, 10, 10)));
		add(getJPasswordFieldConfirm(), new Constraints(new Leading(84, 174,
				10, 10), new Leading(109, 10, 10)));
		add(getJLPasswordConfirmTip(), new Constraints(new Leading(84, 104, 10,
				10), new Leading(144, 10, 10)));
		add(getJBtConfirm(), new Constraints(new Leading(111, 10, 10),
				new Leading(173, 10, 10)));
		setSize(290, 208);
	}

	private JButton getJBtConfirm() {
		if (jBtConfirm == null) {
			jBtConfirm = new JButton();
			jBtConfirm.setText("确认");
			jBtConfirm.addMouseListener(new MouseAdapter() {
	
				public void mouseClicked(MouseEvent event) {
					jBtConfirmMouseMouseClicked(event);
				}
			});
		}
		return jBtConfirm;
	}

	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setToolTipText("请输入密码");
		}
		return jPasswordField;
	}

	private JPasswordField getJPasswordFieldConfirm() {
		if (jPasswordFieldConfirm == null) {
			jPasswordFieldConfirm = new JPasswordField();
			jPasswordFieldConfirm.setToolTipText("请再次输入密码");
			jPasswordFieldConfirm.addFocusListener(new FocusAdapter() {
	
				public void focusLost(FocusEvent event) {
					jPasswordFieldConfirmFocusFocusLost(event);
				}
			});
		}
		return jPasswordFieldConfirm;
	}

	private JTextField getJTFUsername() {
		if (jTFUsername == null) {
			jTFUsername = new JTextField();
		}
		return jTFUsername;
	}

	private JLabel getJLUsername() {
		if (jLUsername == null) {
			jLUsername = new JLabel();
			jLUsername.setText("账  号");
		}
		return jLUsername;
	}

	private JLabel getJLPassword() {
		if (jLPassword == null) {
			jLPassword = new JLabel();
			jLPassword.setText("密  码");
		}
		return jLPassword;
	}

	private JLabel getJLPasswordConfirmTip() {
		if (jLPasswordConfirmTip == null) {
			jLPasswordConfirmTip = new JLabel();
		}
		return jLPasswordConfirmTip;
	}

	private JLabel getJLPasswordConfirm() {
		if (jLPasswordConfirm == null) {
			jLPasswordConfirm = new JLabel();
			jLPasswordConfirm.setText("确认密码");
		}
		return jLPasswordConfirm;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		PropertyConfigurator.configure("spikesc-log4j.properties");
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				InitUserView frame = new InitUserView();
				frame.setDefaultCloseOperation(InitUserView.EXIT_ON_CLOSE);
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jPasswordFieldConfirmFocusFocusLost(FocusEvent event) {
		checkConfirmPassword();
	}
	
	/**
	 * 检查两次输入的密码是否一致。
	 * @return
	 */
	private boolean checkConfirmPassword() {
		if( new String(jPasswordField.getPassword()).equals(new String(jPasswordFieldConfirm.getPassword()))){
			jLPasswordConfirmTip.setText("");
			return true;
		} else {
			jLPasswordConfirmTip.setText("两次密码不一致！");
			return false;
		}
	}

	private void jBtConfirmMouseMouseClicked(MouseEvent event) {
		if (checkConfirmPassword()) {
			if (new UserLogic().createSuperAdmin(jTFUsername.getText(), jPasswordField.getPassword())) {
				JOptionPane.showMessageDialog(this, "初始化管理员成功。");
				LoginView loginView =new LoginView();
				loginView.setDefaultCloseOperation(InitUserView.EXIT_ON_CLOSE);
				loginView.getContentPane().setPreferredSize(loginView.getSize());
				loginView.pack();
				loginView.setLocationRelativeTo(null);
				loginView.setVisible(true);
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "初始化管理员失败。");
			}
		}
	}
}
